package com.gs.m_goods_detail

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.BaseStateCallback
import com.gs.common.constant.StateCode

class GoodsDetailPersistent(context: Context) {

    private val goodsDetailModel by lazy { GoodsDetailModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun getUserInfoByUserId(userId: Int, callback: OnDoGetUserInfoByUserIdStateCallback) {
        Thread{
            goodsDetailModel.getUserInfoByUserId(userId) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetUserInfoByUserIdLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetUserInfoByUserIdSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetUserInfoByUserIdFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun addGoodToCart(goodId: Int, username: String, callback: OnAddGoodToCartStateCallback) {
        Thread{
            goodsDetailModel.addGoodToCart(goodId, username) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onAddGoodToCartLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onAddGoodToCartSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onAddGoodToCartFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun getSoldCount(goodId: Int, callback: OnGetSoldCountStateCallback){
        Thread{
            goodsDetailModel.getSoldCount(goodId){
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetSoldCountLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetSoldCountSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetSoldCountFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    interface OnGetSoldCountStateCallback: BaseStateCallback{
        fun onGetSoldCountLoading(){}
        fun onGetSoldCountSuccess()
        fun onGetSoldCountFailed()
    }

    interface OnDoGetUserInfoByUserIdStateCallback: BaseStateCallback{
        fun onGetUserInfoByUserIdLoading(){}
        fun onGetUserInfoByUserIdSuccess()
        fun onGetUserInfoByUserIdFailed()
    }

    interface OnAddGoodToCartStateCallback: BaseStateCallback {
        fun onAddGoodToCartLoading(){}
        fun onAddGoodToCartSuccess()
        fun onAddGoodToCartFailed()
    }

    companion object{
        private var instance: GoodsDetailPersistent? = null
        fun getInstance(context: Context): GoodsDetailPersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = GoodsDetailPersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}